package com.example.shishaolong.warehousemanager.view;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shishaolong.warehousemanager.R;

/**
 * 自定义标题栏
 */
public class TitleLayout extends LinearLayout {

    //标题栏界面组件的引用
    private TextView tv_title;
    private Button btnBack;
    private Button btnRight;

    public TitleLayout(Context context) {
        super(context,null);
    }

    public TitleLayout(final Context context, AttributeSet attrs) {
        super(context, attrs);

        //引入布局
        LayoutInflater.from(context).inflate(R.layout.title_bar,this);
        btnBack=(Button)findViewById(R.id.btnBack);
        btnRight =(Button)findViewById(R.id.btnEdit);
        btnBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ((Activity)getContext()).finish();
            }
        });

        btnRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,"编辑",Toast.LENGTH_SHORT).show();
            }
        });

        tv_title=(TextView)findViewById(R.id.tv_title);

    }

    //显示活的的标题
    public void setTitle(String title)
    {
        if(!TextUtils.isEmpty(title))
        {
            tv_title.setText(title);
        }
    }

    /**
     * 设置右边按钮的信息
     * @param butotonEntity
     */
    public void  setRightButton(ButotonEntity butotonEntity){
        btnRight.setText(butotonEntity.getName());
        btnRight.setOnClickListener(butotonEntity.getOnClickListener());
    }

    /**
     * 设置返回按钮的相关信息
     * @param butotonEntity
     */
    public void setBtnBack(ButotonEntity butotonEntity){
        btnBack.setText(butotonEntity.getName());
        btnBack.setOnClickListener(butotonEntity.getOnClickListener());
    }

    public static class ButotonEntity{
        private String Name;
        private OnClickListener onClickListener;

        public ButotonEntity(String name, OnClickListener onClickListener) {
            Name = name;
            this.onClickListener = onClickListener;
        }
        public ButotonEntity() {
        }
        public String getName() {
            return Name;
        }
        public void setName(String name) {
            Name = name;
        }
        public OnClickListener getOnClickListener() {
            return onClickListener;
        }
        public void setOnClickListener(OnClickListener onClickListener) {
            this.onClickListener = onClickListener;
        }
    }
}
